package com.iot.ammeter.controller.campus;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iot.ammeter.anno.LogAnnotation;
import com.iot.ammeter.dto.web.campus.BuildingDTO;
import com.iot.ammeter.query.web.campus.BuildingQuery;
import com.iot.ammeter.responseResult.ResponseResult;
import com.iot.ammeter.service.web.campus.BuildingService;
import com.iot.ammeter.utils.EasyExcelExportUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 楼栋 前端控制器
 * </p>
 *
 * @author basil
 * @since 2021-09-25
 */
@RestController
@Api(tags = {"楼栋管理"})
@RequestMapping("/api/building")
public class BuildingController {
    private final BuildingService buildingService;

    public BuildingController(BuildingService buildingService) {
        this.buildingService = buildingService;
    }

    /**
     * 获取楼栋信息列表
     *
     * @param page          请求页码
     * @param pageSize      每页数量
     * @param buildingQuery 楼栋查询条件
     * @return 返回楼栋信息列表
     */
    @ApiOperation("获取楼栋信息列表")
    @LogAnnotation(module = "getBuildingList", operator = "获取楼栋信息列表")
    @PostMapping("/list/{page}/{pageSize}")
    public ResponseResult getBuildingList(@PathVariable int page,
                                          @PathVariable int pageSize,
                                          @RequestBody(required = false) BuildingQuery buildingQuery) {
        /**
         * 数据校验
         */
        if (page <= 0 || pageSize <= 0) {
            return ResponseResult.FAILED("页码参数错误");
        }
        IPage<BuildingDTO> buildingList = buildingService.getBuildingList(page, pageSize, buildingQuery);

        return buildingList.getRecords().size() > 0 ? ResponseResult.SUCCESS(buildingList) : ResponseResult.FAILED("无数据");
    }

    /**
     * 添加楼栋信息
     *
     * @param buildingDTO 楼栋信息
     * @return 添加成功或者添加失败提示
     */
    @ApiOperation("添加楼栋信息")
    @LogAnnotation(module = "addBuilding", operator = "添加楼栋信息")
    @PostMapping("/adjuration")
    public ResponseResult addBuilding(@RequestBody BuildingDTO buildingDTO) {
        String tips = buildingService.addBuilding(buildingDTO);
        return "添加成功".equals(tips) ? ResponseResult.SUCCESS(tips) : ResponseResult.FAILED(tips);
    }

    /**
     * 编辑楼栋信息
     *
     * @param buildingId  楼栋id
     * @param buildingDTO 楼栋信息
     * @return 编辑成功或者编辑失败提示
     */
    @ApiOperation("编辑楼栋信息")
    @LogAnnotation(module = "editBuilding", operator = "编辑楼栋信息")
    @PutMapping("/edit/{buildingId}")
    public ResponseResult editBuilding(@PathVariable String buildingId, @RequestBody BuildingDTO buildingDTO) {
        String tips = buildingService.editBuilding(buildingId, buildingDTO);
        return "更新楼栋信息成功".equals(tips) ? ResponseResult.SUCCESS(tips) : ResponseResult.FAILED(tips);
    }

    /**
     * 批量添加楼栋信息
     *
     * @param buildingDTO 楼栋信息
     * @return 批量添加成功或者失败
     */
    @ApiOperation("批量添加楼栋信息")
    @LogAnnotation(module = "batchAddBuilding", operator = "批量添加楼栋信息")
    @PostMapping("/batch_add")
    public ResponseResult batchAddBuilding(@RequestBody BuildingDTO buildingDTO) {
        String tips = buildingService.batchAddBuilding(buildingDTO);
        return "批量添加成功".equals(tips) ? ResponseResult.SUCCESS(tips) : ResponseResult.FAILED(tips);

    }

    /**
     * 删除楼栋（逻辑删除）
     *
     * @param buildingIds 楼栋id集合
     * @return 删除成功或者失败
     */
    @ApiOperation("删除楼栋信息")
    @LogAnnotation(module = "batchDelBuilding", operator = "删除楼栋信息")
    @DeleteMapping("/delete")
    public ResponseResult batchDelBuilding(@RequestBody String[] buildingIds) {
        String tips = buildingService.batchDelBuilding(buildingIds);
        return "楼栋删除成功".equals(tips) ? ResponseResult.SUCCESS(tips) : ResponseResult.FAILED(tips);
    }

    /**
     * 导出楼栋列表
     *
     * @param page          页码
     * @param pageSize      每页数量
     * @param buildingQuery 查询条件
     * @param response      响应
     */
    @ApiOperation(value = "导出楼栋列表")
    @LogAnnotation(module = "exportBuildingList", operator = "导出楼栋列表")
    @PostMapping("/export_building_list/{page}/{pageSize}")
    public void exportBuildingList(@PathVariable int page, @PathVariable int pageSize,
                                   @RequestBody(required = false) BuildingQuery buildingQuery, HttpServletResponse response) {
        if (page <= 0 || pageSize <= 0) {
            throw new RuntimeException();
        }
        IPage<BuildingDTO> buildingList = buildingService.getBuildingList(page, pageSize, buildingQuery);
        EasyExcelExportUtils.exportToResponse(buildingList.getRecords(), LocalDateTime.now().getDayOfMonth() + "_buildingList.xlsx", "楼栋列表", BuildingDTO.class, response, true);
    }

}
